package builtin

import (
	"talent.com/agency_im/common/msg"
	"talent.com/agency_im/common/pay"
	"talent.com/agency_im/common/user"
	"talent.com/agency_im/conf"
	"talent.com/agency_im/modules/core"
	"talent.com/server/durotar.git/log"
)

type BlacklistInterceptor struct {
}

func (b *BlacklistInterceptor) PreHandle(message *msg.Message) (bool, error) {
	log.Debug("pre handle message on blacklist check, message: %+v", message)
	if message.SendSelf { // 系统自动发送消息放行
		if source, err := user.GetUser(message.SourceID); err != nil {
			return false, err
		} else {
			if source.PlatformID == conf.ServerConfig().Pay.AgencyPlatformID {
				return true, nil
			}
		}
		if isBlacklist, err := pay.IsBlacklist(message.TargetID, message.SourceID); err != nil {
			return false, err
		} else {
			if isBlacklist {
				log.Debug("send message blocked for blacklist, [%v] - [%v]", message.SourceID, message.TargetID)
				core.SendErrorMsg(message.SourceID, "专员已被禁用，若有疑问请联系在线客服")
				return false, nil
			} else {
				return true, nil
			}
		}
	}
	return true, nil
}

func (b *BlacklistInterceptor) PostHandle(message *msg.Message) error {
	return nil
}
